package tamu.edu.server;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tamu.edu.client.TweetPackService;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import twitter4j.conf.PropertyConfiguration;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;


/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class TweetPackServiceImpl extends RemoteServiceServlet implements
    TweetPackService
{

    private TwitterFactory factory = null;

    private static final Map<String, Twitter> urlClientMap = new HashMap<String, Twitter>();
    private static final Map<String, RequestToken> requestTokenMap = new HashMap<String, RequestToken>();


    public TweetPackServiceImpl()
    {
        PropertyConfiguration conf = new PropertyConfiguration( getClass().getClassLoader().getResourceAsStream(
            "twitter4j.properties" ) );
        factory = new TwitterFactory( conf );
    }


    public String getReqTokenLink() throws Exception
    {
        try
        {
            Twitter twitter = factory.getInstance();
            RequestToken requestToken = twitter.getOAuthRequestToken();

            String url = requestToken.getAuthorizationURL();

            urlClientMap.put( url, twitter );

            return url;
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            throw e;
        }
    }


    public List<String> getTweets( String url, String pin ) throws Exception
    {
        try
        {
            Twitter twitter = urlClientMap.get( url );
            RequestToken reqToken = requestTokenMap.get( url );

            AccessToken accessToken = twitter.getOAuthAccessToken( reqToken, pin );

            ResponseList<Status> statuses = twitter.getUserTimeline();
            List<String> tweets = new ArrayList<String>();
            for( Status t : statuses )
            {
                tweets.add( t.getText() );
            }
            
            return tweets;
        }
        catch ( Exception e )
        {
            e.printStackTrace();
            throw e;
        }
    }


    /**
     * Escape an html string. Escaping data received from the client helps to
     * prevent cross-site script vulnerabilities.
     *
     * @param html the html string to escape
     * @return the escaped string
     */
    private String escapeHtml( String html )
    {
        if ( html == null )
        {
            return null;
        }
        return html.replaceAll( "&", "&amp;" ).replaceAll( "<", "&lt;" ).replaceAll(
            ">", "&gt;" );
    }
    
    public static void main( String[] args ) throws Exception
    {
        TweetPackServiceImpl t = new TweetPackServiceImpl();
        String s = t.getReqTokenLink();
        System.out.println( s );
    }
}
